home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src / ace / c / serial.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-04  |  5.8 KB  |  266 lines

  1. /* << ACE >>
  2.  
  3.    -- Amiga BASIC Compiler --
  4.  
  5.    ** Parser: serial port commands **
  6.    ** Copyright (C) 1998 David Benn
  7.    ** 
  8.    ** This program is free software; you can redistribute it and/or
  9.    ** modify it under the terms of the GNU General Public License
  10.    ** as published by the Free Software Foundation; either version 2
  11.    ** of the License, or (at your option) any later version.
  12.    **
  13.    ** This program is distributed in the hope that it will be useful,
  14.    ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    ** GNU General Public License for more details.
  17.    **
  18.    ** You should have received a copy of the GNU General Public License
  19.    ** along with this program; if not, write to the Free Software
  20.    ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    Author: David J Benn
  23.      Date: 5th,6th September 1993
  24. */
  25.  
  26. #include "acedef.h"
  27.  
  28. /* locals */
  29. static    char     *frame_ptr[] = { "(a4)","(a5)" };
  30.  
  31. /* externals */
  32. extern    int    sym;
  33. extern    int    lev;
  34. extern    int    obj;
  35. extern    int    typ;
  36. extern    SYM    *curr_item;
  37. extern    char       id[MAXIDSIZE]; 
  38.  
  39. /* functions */
  40. void serial_command()
  41. {
  42. /* parse a serial command */
  43.  
  44.  insymbol();
  45.  
  46.  switch(sym)
  47.  {
  48.       case opensym:    open_serial();                
  49.             break;
  50.  
  51.       case closesym:    close_serial();                
  52.             break;
  53.  
  54.       case readsym:    read_serial();                
  55.             break;
  56.  
  57.       case writesym:    write_serial();                
  58.             break;
  59.  
  60.     default:    _error(75);    /* open,close etc expected */
  61.             break;
  62.  }
  63. }
  64.  
  65. void open_serial()
  66. {
  67. /* open a channel to a serial port.
  68.     
  69.    SERIAL OPEN [#] channel,unit,baud,"N|E|O|M|S<d><s>[A][X]"[,size][,name]
  70.  
  71.    where:     channel    = unique channel number 
  72.         unit    = unit of serial device to use
  73.         baud    = baud rate
  74.         NEOMS     = parity: None, Even, Odd, Mark, Space
  75.         <d>    = # of data bits (1 digit)
  76.         <s>    = # of stop bits (1 digit)
  77.         A    = serial device will use ALL 7 port wires
  78.         X     = sets Xon/Xoff mode
  79.         size    = size of serial buffer
  80.         name    = name of serial device        
  81.  */
  82.  
  83.  insymbol();
  84.  
  85.  if (sym == hash) insymbol();    /* # */
  86.  
  87.  if (make_integer(expr()) == shorttype) make_long();    /* channel */ 
  88.           
  89.  if (sym != comma) _error(16);
  90.  else
  91.  {  
  92.   insymbol();
  93.   if (make_integer(expr()) == shorttype) make_long();    /* unit */ 
  94.           
  95.   if (sym != comma) _error(16);
  96.   else
  97.   {
  98.    insymbol();
  99.    if (make_integer(expr()) == shorttype) make_long();    /* baud */ 
  100.           
  101.    if (sym != comma) _error(16);
  102.    else
  103.    {
  104.     insymbol();
  105.     if (expr() != stringtype) _error(4);    /* parameter string */
  106.     else
  107.     {
  108.      /* optional serial READ buffer size */
  109.      if (sym == comma)
  110.      {
  111.       insymbol();
  112.       if (sym != comma)
  113.       {
  114.          if (make_integer(expr()) == shorttype) 
  115.         make_long();             /* Read buffer size */ 
  116.       }
  117.       else
  118.      gen("move.l","#512","-(sp)");        /* defaults to 512 bytes */    
  119.      }    
  120.      else 
  121.          gen("move.l","#512","-(sp)");        /* defaults to 512 bytes */
  122.  
  123.      /* optional serial device name */
  124.      if (sym == comma)
  125.      {
  126.       insymbol();
  127.       if (expr() != stringtype) _error(4);    /* serial device name */
  128.      }
  129.      else
  130.      gen("move.l","#0","-(sp)");        /* defaults to NULL */
  131.  
  132.      /* call open_serial function */
  133.      gen("jsr","_OpenSerial","  ");
  134.      gen("add.l","#24","sp");
  135.      enter_XREF("_OpenSerial");      
  136.     }
  137.    }
  138.   }
  139.  }
  140.  
  141. void close_serial()
  142. {
  143. /* close a channel to a serial port. 
  144.  
  145.    SERIAL CLOSE [#] channel
  146. */
  147.  
  148.  insymbol();
  149.  
  150.  if (sym == hash) insymbol();    /* # */
  151.  
  152.  if (make_integer(expr()) == shorttype) make_long();    /* channel */ 
  153.           
  154.  gen("jsr","_CloseSerial","  ");
  155.  gen("addq","#4","sp");
  156.  enter_XREF("_CloseSerial"); 
  157. }
  158.  
  159. void read_serial()
  160. {
  161. SYM  *storage;
  162. char addrbuf[40];
  163.  
  164. /* read a specified number of bytes into a buffer.
  165.  
  166.    SERIAL READ [#] channel,buffer,length
  167. */
  168.  
  169.  insymbol();
  170.  
  171.  if (sym == hash) insymbol();    /* # */
  172.  
  173.  if (make_integer(expr()) == shorttype) make_long();    /* channel */ 
  174.  
  175.  if (sym != comma) _error(16);
  176.  else
  177.  {
  178.   insymbol();                        
  179.   if (sym == ident && obj == variable)            /* buffer */
  180.   {
  181.    /* if string variable/array doesn't exist, create a simple variable */
  182.    if (!exist(id,variable) && !exist(id,array)) 
  183.    {
  184.     /* allocate a simple string variable */
  185.     enter(id,typ,obj,0);
  186.     enter_DATA("_nullstring:","dc.b 0");
  187.     gen("pea","_nullstring","  ");
  188.     assign_to_string_variable(curr_item,MAXSTRLEN);
  189.    }
  190.  
  191.    storage=curr_item;
  192.  
  193.    /* is it a string variable or array? */
  194.    if (storage->type != stringtype) _error(4);
  195.    else    
  196.    {
  197.     /* get address of string pointed to by variable/array element */
  198.     itoa(-1*storage->address,addrbuf,10);
  199.     strcat(addrbuf,frame_ptr[lev]);
  200.  
  201.     /* pass string address to function (on stack) */
  202.     if (storage->object == array)
  203.     {
  204.      point_to_array(storage,addrbuf);
  205.      gen("move.l",addrbuf,"d0");
  206.      gen("add.l","d7","d0");
  207.      gen("move.l","d0","-(sp)");
  208.     }
  209.      else
  210.            gen("move.l",addrbuf,"-(sp)");
  211.  
  212.     insymbol();
  213.     if (sym != comma) _error(16);
  214.     else
  215.     {     
  216.      insymbol();
  217.      if (make_integer(expr()) == shorttype) make_long();      /* length */ 
  218.  
  219.      /* call serial_read function */
  220.      gen("jsr","_ReadSerial","  ");
  221.      gen("add.l","#12","sp");
  222.      enter_XREF("_ReadSerial");
  223.     }
  224.    }
  225.   }
  226.   else _error(19); /* variable (or array) expected */      
  227.  }
  228.  insymbol();
  229. }
  230.  
  231. void write_serial()
  232. {
  233. /* write a specified number of bytes from a buffer.
  234.  
  235.    SERIAL WRITE [#] channel,buffer,length
  236. */
  237.  
  238.  insymbol();
  239.  
  240.  if (sym == hash) insymbol();    /* # */
  241.  
  242.  if (make_integer(expr()) == shorttype) make_long();    /* channel */ 
  243.  
  244.  if (sym != comma) _error(16);
  245.  else
  246.  {
  247.   insymbol();
  248.   if (expr() != stringtype) _error(4);            /* buffer */
  249.   else
  250.   {        
  251.    if (sym != comma) _error(16);
  252.    else
  253.    {     
  254.     insymbol();
  255.     if (make_integer(expr()) == shorttype) make_long();    /* length */ 
  256.  
  257.     /* call serial_write function */
  258.     gen("jsr","_WriteSerial","  ");
  259.     gen("add.l","#12","sp");
  260.     enter_XREF("_WriteSerial");
  261.    }          
  262.   }
  263.  }
  264. }
  265.